package com.vpx.server.framework.handler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * @Title CustomDateHandler
 * @Description 自定义日期处理器（针对sqlite 日期转换问题）
 * @Author wangyan
 * @Date 2023/4/11  23:06
 */
public class CustomDateHandler extends BaseTypeHandler<Date> {
    //将java类型转换成数据库需要的类型
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
        long time = date.getTime();
        preparedStatement.setLong(i, time);
    }

    //将数据库类型转换成java需要的类型
    @Override
    public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
        long along = resultSet.getLong(s);
        Date date = new Date(along);
        return date;
    }

    @Override
    public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
        long along = resultSet.getLong(i);
        Date date = new Date(along);
        return date;
    }

    @Override
    public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        long along = callableStatement.getLong(i);
        Date date = new Date(along);
        return date;
    }
}
